package io.github.talelin.latticy.service.K;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.enumeration.JcDwxxbRootOrNotEnum;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.dto.K.JcDwxxbDTO;
import io.github.talelin.latticy.mapper.K.JcDwxxbMapper;
import io.github.talelin.latticy.model.K.JcDwxxbDO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator@TaleLin
 * @since 2021-06-08
 */
@Service
public class JcDwxxbService extends ServiceImpl<JcDwxxbMapper, JcDwxxbDO> {

    @Autowired
    private JcDwxxbMapper JcDwxxbMapper;

    public JcDwxxbDO getById(Integer id) {
        JcDwxxbDO b_data = JcDwxxbMapper.selectById(id);
        return b_data;
    }

    public void delete(long id) {
        JcDwxxbDO exist = this.getById(id);
        if (exist == null) {
            throw new NotFoundException(70000);
        }
        this.getBaseMapper().deleteById(id);
    }

    public void update(JcDwxxbDTO dto, Long id) {
        JcDwxxbDO JcDwxxbDO = this.getById(id);
        if (JcDwxxbDO == null) {
            throw new NotFoundException(70000);
        }
        BeanUtils.copyProperties(dto, JcDwxxbDO);
        this.updateById(JcDwxxbDO);
    }

    public List<JcDwxxbDO> getJcDwxxbByKeyword(String q) {
        List<JcDwxxbDO> JcDwxxbs = JcDwxxbMapper.selectByNameLikeKeyword(q);
        return JcDwxxbs;
    }

    public IPage<JcDwxxbDO> seachJcDwxxb(Page<JcDwxxbDO> pager, String keyword){
        QueryWrapper<JcDwxxbDO> wrapper = new QueryWrapper<>();
        wrapper.like("DWMC",keyword);
        IPage<JcDwxxbDO> iPage = JcDwxxbMapper.selectPage(pager,wrapper);
        return iPage;
    }

    public IPage<JcDwxxbDO> getCategoriesByPage(Integer count, Integer page, Integer root) {
        Page<JcDwxxbDO> pager = new Page<>(page, count);
        QueryWrapper<JcDwxxbDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(JcDwxxbDO::getIsRoot, root);
        return this.getBaseMapper().selectPage(pager, wrapper);
    }

    public IPage<JcDwxxbDO> getSubCategoriesByPage(Integer count, Integer page, Integer id) {
        Page<JcDwxxbDO> pager = new Page<>(page, count);
        QueryWrapper<JcDwxxbDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(JcDwxxbDO::getIsRoot, JcDwxxbRootOrNotEnum.NOT_ROOT.getValue());
        wrapper.lambda().eq(JcDwxxbDO::getParentId, id);
        return this.getBaseMapper().selectPage(pager, wrapper);
    }
}
